package com.songlanyun.modules.goodsuserdiscount.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.modules.goodsuserdiscount.entity.GoodsUserDiscountDetail;
import com.songlanyun.modules.goodsuserdiscount.model.dto.GoodsUserDiscountConfigDTO;
import com.songlanyun.modules.shop.entity.ShopUserRoleConfigEntity;
import com.songlanyun.modules.shop.service.ShopUserRoleConfigService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.goodsuserdiscount.entity.GoodsUserDiscountConfig;
import com.songlanyun.modules.goodsuserdiscount.service.GoodsUserDiscountConfigService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;


/**
 * 商品会员折扣配置
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-13 09:54:23
 */
@RestController
    @RequestMapping("admin/goodsuserdiscount")
public class GoodsUserDiscountConfigAdminController extends AbstractController {
    @Autowired
    private GoodsUserDiscountConfigService goodsUserDiscountConfigService;

    @Autowired
    private ShopUserRoleConfigService shopUserRoleConfigService;


    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}")
    //@RequiresPermissions("goodsuserdiscount:productgoodsuserdiscountconfig:info")
    public R info(@PathVariable("goodsId") Long goodsId) {


        GoodsUserDiscountConfig config = goodsUserDiscountConfigService.getByGoodsId(goodsId);
        if (config == null) {
            List<ShopUserRoleConfigEntity> shopRoles = shopUserRoleConfigService.select(getShopId());
            List<GoodsUserDiscountDetail> collect = shopRoles.stream().map(o -> {
                GoodsUserDiscountDetail detail = new GoodsUserDiscountDetail();
                detail.setRoleName(o.getRoleName());
                detail.setDiscount(o.getDiscount());
                detail.setRoleCode(o.getRoleCode());
                return detail;
            }).collect(Collectors.toList());
            config = new GoodsUserDiscountConfig();
            config.setUseDefault(true);
            config.setDetails(collect);
        }

        return R.ok().put("data", config);
    }

    /**
     * 给商品设置自定义的会员等级折扣
     */
    @SysLog("给商品设置自定义的会员等级折扣")
    @PostMapping("/set/{goodsId}")
    //@RequiresPermissions("goodsuserdiscount:productgoodsuserdiscountconfig:save")
    public R set(@PathVariable("goodsId") Long goodsId, @RequestBody GoodsUserDiscountConfigDTO dto) {
        GoodsUserDiscountConfig config = goodsUserDiscountConfigService.set(getShopId(), goodsId, dto);
        return R.ok().put("data", config);
    }


}
